package tt.dz.service.imp.charger;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tt.dz.dao.ext.charger.OmsChargerErrorRecordDao;
import tt.dz.entity.base.JsonCommon;
import tt.dz.entity.base.JsonForList;
import tt.dz.entity.base.JsonPageForList;
import tt.dz.entity.base.OmsCommonParam;
import tt.dz.entity.charger.OmsChargerErrorRecord;
import tt.dz.entity.station.OmsStation;
import tt.dz.service.charger.OmsChargerErrorRecordService;
import tt.dz.service.imp.base.BaseService;
import tt.dz.util.OmsConstant;
import tt.dz.util.StringUtil;
import tt.dz.util.OmsConstant.MsgCodeEnum;
@Service @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class OmsChargerErrorRecordServiceImpl extends BaseService implements OmsChargerErrorRecordService
{

	@Resource
	OmsChargerErrorRecordDao omsChargerErrorRecordDao;
	
	@Override
	public JsonCommon<?> add(OmsChargerErrorRecord obj) {
		int result = omsChargerErrorRecordDao.save(obj);
		if(result>0){
			return new JsonCommon(OmsConstant.SuccessCodeEnum.SuccessCode.value(), OmsConstant.SuccessCodeEnum.SuccessCode.desc());
		}
		return new JsonCommon(OmsConstant.ErrorCodeEnum.SqlExecException.value(), OmsConstant.ErrorCodeEnum.SqlExecException.desc());
	}

	@Override
	public JsonCommon<?> update(OmsChargerErrorRecord obj) {
		int result = omsChargerErrorRecordDao.updateObject(obj);
		if(result>0){
			return new JsonCommon(OmsConstant.SuccessCodeEnum.SuccessCode.value(), OmsConstant.SuccessCodeEnum.SuccessCode.desc());
		}
		return new JsonCommon(OmsConstant.ErrorCodeEnum.SqlExecException.value(), OmsConstant.ErrorCodeEnum.SqlExecException.desc());
	}
	
	@Override
	public JsonCommon<?> deleteLogic(String charger_error_id) {
		OmsChargerErrorRecord query_obj = new OmsChargerErrorRecord();
		query_obj.setCharger_error_id(Long.valueOf(charger_error_id));
		OmsChargerErrorRecord obj = omsChargerErrorRecordDao.findOneExample(query_obj, null, null);
		if(obj!=null){
			int result = omsChargerErrorRecordDao.LogicDeleteObject(obj);
			if(result>0){
				return new JsonCommon(OmsConstant.SuccessCodeEnum.SuccessCode.value(), OmsConstant.SuccessCodeEnum.SuccessCode.desc());
			}
			return new JsonCommon(OmsConstant.ErrorCodeEnum.SqlExecException.value(), OmsConstant.ErrorCodeEnum.SqlExecException.desc());
		}
		return new JsonCommon(OmsConstant.ErrorCodeEnum.SqlExecException.value(), OmsConstant.ErrorCodeEnum.SqlExecException.desc());
	}

	@Override
	public JsonForList<?> findList(OmsChargerErrorRecord obj) {
		List<OmsChargerErrorRecord> list = omsChargerErrorRecordDao.findExample(obj, null, null, null, null);
		return new JsonForList<>(list, OmsConstant.SuccessCodeEnum.SuccessCode.value(), OmsConstant.SuccessCodeEnum.SuccessCode.desc());
	}

	@Override
	public OmsChargerErrorRecord findOneExample(OmsChargerErrorRecord omsChargerErrorRecord,String order, String sortVal) {
		OmsChargerErrorRecord  obj = omsChargerErrorRecordDao.findOneExample(omsChargerErrorRecord, null, null);
		return obj;
	}

	@Override
	public List<OmsChargerErrorRecord> findListByCondition(String charger_id) {
		String condition = " where charger_id='" + charger_id + "' order by create_time desc limit 0,10 ";
		List<OmsChargerErrorRecord> list = omsChargerErrorRecordDao.findListByCondition(new OmsChargerErrorRecord(), condition);
		return list;
	}

	@Override
	public JsonPageForList findPageList(OmsCommonParam buildQueryParam, String searchStr) {
		try
		{
			StringBuffer sql = new StringBuffer();
			List<Map<String, Object>> list_ErrorRecord = null;		
	
			sql.append("select charger_error_id,charger_id,error_code,error_msg,create_time,handle_text,status ");
			sql.append("from oms_charger_error_record where valid_flag=1 ");
			
			if (StringUtil.isNotBlank(searchStr)) {
				sql.append(" and (charger_id like '%").
					append(searchStr).append("%' or error_code like '%").
					append(searchStr).append("%' or error_msg like '%").
					append(searchStr).append("%' or handle_text like '%").
					append(searchStr).append("%')");
			}
			
			int total = 0;
			if (buildQueryParam.isQueryTotal())
			{
				try {
					total =  omsChargerErrorRecordDao.queryTotalBySql(new OmsChargerErrorRecord(), "select count(1) from ("+sql.toString()+") a", new ArrayList<Object>());
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			//分页查询匹配
			if(buildQueryParam.getPageNo()!=null && buildQueryParam.getPageSize()!=null && buildQueryParam.getPageSize().intValue()!=0){
				sql.append(" limit ");
				sql.append((buildQueryParam.getPageNo()-1)*buildQueryParam.getPageSize());
				sql.append(",");
				sql.append(buildQueryParam.getPageSize());
			}
			
			list_ErrorRecord = omsChargerErrorRecordDao.findDataBySql(sql.toString());
	
			JsonPageForList jfl = new JsonPageForList();
			jfl.setCode(MsgCodeEnum.SUCCESS.getMsgCode());
			jfl.setList(list_ErrorRecord);
			jfl.setTotal(total);
			jfl.setMsg(MsgCodeEnum.SUCCESS.getMsgInfo());
			return jfl;
		
		}
		catch (Exception e)
		{
			JsonPageForList jfl = new JsonPageForList();
			jfl.setCode(MsgCodeEnum.ERROR400.getMsgCode());
			jfl.setMsg(MsgCodeEnum.ERROR400.getMsgInfo());
			return jfl;
		}
	}
	
}
